package com.example.watertest;



import java.io.IOException;

import org.andengine.audio.music.Music;
import org.andengine.audio.music.MusicFactory;
import org.andengine.audio.sound.Sound;
import org.andengine.audio.sound.SoundFactory;
import org.andengine.engine.Engine;
import org.andengine.engine.FixedStepEngine;
import org.andengine.engine.camera.Camera;
import org.andengine.engine.handler.physics.PhysicsHandler;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.WakeLockOptions;
import org.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;
import org.andengine.entity.IEntity;
import org.andengine.entity.modifier.IEntityModifier;
import org.andengine.entity.modifier.IEntityModifier.IEntityModifierListener;
import org.andengine.entity.modifier.IEntityModifier.IEntityModifierMatcher;
import org.andengine.entity.modifier.JumpModifier;
import org.andengine.entity.modifier.LoopEntityModifier;
import org.andengine.entity.modifier.ScaleModifier;
import org.andengine.entity.modifier.SequenceEntityModifier;
import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.scene.IOnSceneTouchListener;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.text.Text;
import org.andengine.extension.physics.box2d.FixedStepPhysicsWorld;
import org.andengine.extension.physics.box2d.PhysicsConnector;
import org.andengine.extension.physics.box2d.PhysicsFactory;
import org.andengine.extension.physics.box2d.PhysicsWorld;
import org.andengine.extension.physics.box2d.util.Vector2Pool;
import org.andengine.input.sensor.acceleration.AccelerationData;
import org.andengine.input.sensor.acceleration.IAccelerationListener;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.font.FontFactory;
import org.andengine.opengl.texture.ITexture;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.opengl.util.GLState;
import org.andengine.ui.activity.BaseGameActivity;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import org.andengine.util.adt.color.Color;
import org.andengine.util.modifier.IModifier;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.FixtureDef;

import android.hardware.SensorManager;
import android.util.Log;




public class PhysicsApplicationII extends BaseGameActivity implements IOnSceneTouchListener {

	 public static int cameraWidth = 800;
	  public static int cameraHeight = 480;
	  public Scene mScene;
	  public PhysicsWorld mPhysicsWorld;
	  private int mBodyCount = 0;
	private Body body ;
	  public static final short CATEGORYBIT_DEFAULT = 1;
	  public static final short CATEGORYBIT_RED_BOX = 2;
	  public static final short CATEGORYBIT_GREEN_BOX = 4;
	  public static final short MASKBITS_RED_BOX = CATEGORYBIT_DEFAULT + CATEGORYBIT_RED_BOX;
	  public static final short MASKBITS_GREEN_BOX = CATEGORYBIT_DEFAULT + CATEGORYBIT_GREEN_BOX;
	  public static final FixtureDef RED_BOX_FIXTURE_DEF = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f, false, CATEGORYBIT_RED_BOX, MASKBITS_RED_BOX, (short)0);
	  public static final FixtureDef GREEN_BOX_FIXTURE_DEF = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f, false, CATEGORYBIT_GREEN_BOX, MASKBITS_GREEN_BOX, (short)0); 
	
	  @Override
	  public Engine onCreateEngine(final EngineOptions
	      pEngineOptions) {
	    return new FixedStepEngine(pEngineOptions, 60);
	  }

	  @Override
	  public EngineOptions onCreateEngineOptions() {
	    EngineOptions engineOptions = new EngineOptions(true,
	      ScreenOrientation.LANDSCAPE_SENSOR, new
	        FillResolutionPolicy(), new Camera(0,
	          0, cameraWidth, cameraHeight));
	    engineOptions.getRenderOptions().setDithering(true);
	    engineOptions.getRenderOptions().
	      getConfigChooserOptions()
	        .setRequestedMultiSampling(true);
	    engineOptions.setWakeLockOptions(
	      WakeLockOptions.SCREEN_ON);
	    return engineOptions;
	  }

	  @Override
	  public void onCreateResources(OnCreateResourcesCallback pOnCreateResourcesCallback) {
	    pOnCreateResourcesCallback.
	      onCreateResourcesFinished();
	  }

	  @Override
	  public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback) {
	    mScene = new Scene();
	    mScene.setBackground(new Background(0.9f,0.9f,0.9f));
	    pOnCreateSceneCallback.onCreateSceneFinished(mScene);
	
	  }
	  
	  private void addBody(final float pX, final float pY) {
		  this.mBodyCount++;
		  final Rectangle rectangle = new Rectangle(pX, pY, 50f, 50f,
		    this.getVertexBufferObjectManager());
		  rectangle.setAlpha(0.5f);
		   
		  if(this.mBodyCount % 2 == 0) {
		    rectangle.setColor(1f, 0f, 0f);
		    body = PhysicsFactory.createBoxBody(this.mPhysicsWorld,
		      rectangle, BodyType.DynamicBody, RED_BOX_FIXTURE_DEF);
		  } else {
		    rectangle.setColor(0f, 1f, 0f);
		    body = PhysicsFactory.createBoxBody(this.mPhysicsWorld,
		      rectangle, BodyType.DynamicBody, GREEN_BOX_FIXTURE_DEF);
		  }
		  
		  this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector( rectangle, body, true, true));
		  this.mScene.attachChild(rectangle);
		}
	  
	  @Override
	  public void onPopulateScene(Scene pScene, OnPopulateSceneCallback pOnPopulateSceneCallback) {
		  
		  mScene.setOnSceneTouchListener(this);
		  pOnPopulateSceneCallback.onPopulateSceneFinished();
				
	  }

	  @Override
	  public boolean onSceneTouchEvent(Scene pScene, TouchEvent
	      pSceneTouchEvent) {
		  mPhysicsWorld = new PhysicsWorld(new Vector2(0,-10), false);
	  		mScene.registerUpdateHandler(mPhysicsWorld);
		  if(this.mPhysicsWorld != null)
			  if(pSceneTouchEvent.isActionDown())
			    this.addBody(pSceneTouchEvent.getX(),
			      pSceneTouchEvent.getY());
		 
		  /*
		   * tipos de Fuerzas
		   * */
		  
		  //body.applyTorque(2000f);
		  /*body.applyForce(0f, 500f, 
				  body.getWorldCenter().x, 
				  body.getWorldCenter().y);*/
		  /*body.applyLinearImpulse(0f, 200f, 
				  body.getWorldCenter().x, 
				  body.getWorldCenter().y);*/
		  //body.setLinearVelocity(0f, 20f);
		  body.applyAngularImpulse(20f);
		  body.setAngularVelocity(10f);
		  
	    return true;
	  }

	
		
	
	
}
